package com.liruo.enumconverter.core.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.liruo.enumconverter.model.SourceEnum;

import java.io.IOException;

public class SourceEnumJackSerializer extends JsonSerializer<SourceEnum<?>> {
    @Override
    public void serialize(SourceEnum<?> sourceEnum, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(sourceEnum == null){
            jsonGenerator.writeNull();
        }else{
            Object source = sourceEnum.source();
            if(source instanceof Integer){
                jsonGenerator.writeNumber((Integer) source);
            }else{
                jsonGenerator.writeString((String) source);
            }
        }
    }
}